package com.infore.water.service;

import com.github.pagehelper.PageHelper;
import com.infore.water.dao.DataDao;
import com.infore.water.dao.UserDao;
import com.infore.water.entity.AlarmRecord;
import com.infore.water.entity.AlarmStandard;
import com.infore.water.mapper.DataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:wk
 * @date:Created in 14:10 2017\11\14 0014
 */
@Service
public class AlarmService {

    @Autowired(required = false)
    private DataMapper dataMapper;

    @Autowired(required = false)
    private DataDao dataDao;
    @Autowired
    private UserDao userDao;
    public List<AlarmRecord> getAlarmRecord(String siteId, String stationId, String alarmType, String pageNum, String pageSize) throws ParseException {
        List<AlarmRecord> list;
        PageHelper.startPage(Integer.parseInt(pageNum)+1,Integer.parseInt(pageSize));
        if("0".equals(alarmType)){
            if("0".equals(stationId)){
                list = dataMapper.getAlarmRecordWithoutStationId();
            }else {
                list = dataMapper.getAlarmRecord(Integer.parseInt(stationId), Integer.parseInt(siteId));
            }
        }else if("1".equals(alarmType)){
            if("0".equals(stationId)){
                list = dataMapper.getAlarmRecordByTypeWithoutStationId("002");
            }else {
                list = dataMapper.getAlarmRecordByType(Integer.parseInt(stationId),Integer.parseInt(siteId),"002");
            }
        }else if("2".equals(alarmType)) {
            if("0".equals(stationId)) {
                list = dataMapper.getAlarmRecordByTypeWithoutStationId( "001");
            }else {
                list = dataMapper.getAlarmRecordByType(Integer.parseInt(stationId), Integer.parseInt(siteId), "001");
            }
        }else {
            if("0".equals(stationId)) {
                list = dataMapper.getAlarmRecordByTypeWithoutStationId( "003");
            }else {
                list = dataMapper.getAlarmRecordByType(Integer.parseInt(stationId), Integer.parseInt(siteId), "003");
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        for(int i=0;i<list.size();i++){
            if("sms".equals(list.get(i).getAlarmStep())){
                list.get(i).setAlarmStep("短信提醒");
            }else {
                list.get(i).setAlarmStep("平台提示");
            }
            list.get(i).setTime(simpleDateFormat.format(sdf.parse(list.get(i).getTime())));

            if("001".equals(list.get(i).getAlarmType())){
                list.get(i).setAlarmType("数据超标");
            }else if("002".equals(list.get(i).getAlarmType())){
                list.get(i).setAlarmType("数据异常");
            }else {
                list.get(i).setAlarmType("站点离线");
            }
        }
        return list;
    }

    public List getStandardList(String siteId, String stationId, String alarmType, String pageNum, String pageSize) {
        List<AlarmStandard> list;
        if("0".equals(alarmType)){
            if("0".equals(stationId)) {
                list = dataMapper.getStandardListWithoutStationId();
            }else {
                list = dataMapper.getStandardList(Integer.parseInt(stationId), Integer.parseInt(siteId));
            }
        }else if("1".equals(alarmType)){
            if("0".equals(stationId)){
                list = dataMapper.getStandardListByTypeWithoutStationId("002");
            }else {
                list = dataMapper.getStandardListByType(Integer.parseInt(stationId),Integer.parseInt(siteId),"002");
            }

        }else if("2".equals(alarmType)) {
            if("0".equals(stationId)){
                list = dataMapper.getStandardListByTypeWithoutStationId("001");
            }else {
                list = dataMapper.getStandardListByType(Integer.parseInt(stationId),Integer.parseInt(siteId),"001");
            }
        }else {
            if("0".equals(stationId)){
                list = dataMapper.getStandardListByTypeWithoutStationId("003");
            }else {
                list = dataMapper.getStandardListByType(Integer.parseInt(stationId),Integer.parseInt(siteId),"003");
            }
        }
        List<AlarmStandard> dataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i >= (Integer.parseInt(pageNum) * Integer.parseInt(pageSize)) && i < ((Integer.parseInt(pageNum) + 1) * Integer.parseInt(pageSize))) {
                List<Map<String,Object>> personList = dataMapper.getAlarmPersonByAlarmId(list.get(i).getId());
                String personStr="";
                String personId="";
                for(int j=0;j<personList.size();j++){
                    personStr+=personList.get(j).get("name")+";";
                    if(j!=personList.size()-1){
                        personId+=personList.get(j).get("id")+",";
                    }else {
                        personId+=personList.get(j).get("id");
                    }
                }
                list.get(i).setAlarmPersonIds(personId);
                list.get(i).setAlarmPerson(personStr);
                if("sms".equals(list.get(i).getAlarmStep())){
                    list.get(i).setAlarmStep("短信报警");
                }else {
                    list.get(i).setAlarmStep("平台提醒");
                }
                if("001".equals(list.get(i).getAlarmType())){
                    list.get(i).setAlarmType("数据超标");
                }else if("002".equals(list.get(i).getAlarmType())){
                    list.get(i).setAlarmType("数据异常");
                }else {
                    list.get(i).setAlarmType("站点离线");
                }
                dataList.add(list.get(i));
            }
        }
        return dataList;
    }

    public List getStationProject(String userId) {
        Map<String,Object> userMap = userDao.queryUserByUserId(userId);
        List<Map<String,Object>> list = dataMapper.getStationProject(userMap.get("roleId").toString());
        List<Map<String,Object>> dataList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            boolean flag = true;
            for(int j=0;j<dataList.size();j++){
                if(dataList.get(j).get("stationId").equals(list.get(i).get("stationId"))){
                    flag = false;
                    Map<String,Object> proMap = new HashMap<>();
                    proMap.put("projectId",list.get(i).get("projectId"));
                    proMap.put("projectName",list.get(i).get("projectName"));
                    ((List<Map<String,Object>>)dataList.get(j).get("proList")).add(proMap);
                }
            }
            if(flag){
                Map<String,Object> map = new HashMap<>();
                map.put("stationId",list.get(i).get("stationId"));
                map.put("stationName",list.get(i).get("stationName"));
                List<Map<String,Object>> proList = new ArrayList<>();
                Map<String,Object> proMap = new HashMap<>();
                proMap.put("projectId",list.get(i).get("projectId"));
                proMap.put("projectName",list.get(i).get("projectName"));
                proList.add(proMap);
                map.put("proList",proList);
                dataList.add(map);
            }
        }
        return dataList;
    }

    public List getAlarmPerson() {
        List list = dataMapper.getAlarmPerson();
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer setAlarmStandard(String alarmId, String stationId, String alarmType, String alarmLevel, String alarmStep, String alarmPerson, String workDay, String time, String interval) {
        int num = 0 ;
        String type = "";
        if("0".equals(alarmType)){
            type="001";
        }else if("1".equals(alarmType)){
            type="002";
        }else {
            type="003";
        }
        if((alarmId!=null)&&(!"0".equals(alarmId))){
            num = dataMapper.updateAlarmStandard(type,"sms",workDay,time,interval,alarmLevel,Integer.parseInt(stationId),Integer.parseInt(alarmId));
            String[] person = alarmPerson.split(",");
            dataMapper.deleteAlarmPerson(Integer.parseInt(alarmId));
            for(int i=0;i<person.length;i++){
                dataMapper.updateAlarmPerson(Integer.parseInt(alarmId),Integer.parseInt(person[i]));
            }
        }else {
            List<Map<String,Object>> dList = dataMapper.getAlarmStandardByStationId(Integer.parseInt(stationId),type);
            if(dList.size()!=0){
                return -1;
            }
           Map<String,Object> map = new HashMap<>();
           map.put("stationId",stationId);
           map.put("alarmLevel",alarmLevel);
           map.put("time",time);
            map.put("workDay",workDay);
            map.put("interval",interval);
            map.put("alarmStep","sms");
            map.put("alarmType",type);
            int alarmStandardId = dataDao.setAlarmStandard(map);
            String[] person = alarmPerson.split(",");
            for(int i=0;i<person.length;i++){
                dataMapper.updateAlarmPerson(alarmStandardId,Integer.parseInt(person[i]));
            }
        }
        return num;
    }

    public void deleteAlarmRecord(String alarmId) {
        dataMapper.deleteAlarmRecord(Integer.parseInt(alarmId));
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteAlarmStandard(String alarmId) {
        dataMapper.deleteAlarmPerson(Integer.parseInt(alarmId));
        dataMapper.deleteAlarmStandard(Integer.parseInt(alarmId));
    }
}
